package com.app.mingluxing.mqtt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.app.mingluxing.R;
import com.app.mingluxing.mqttsource.MqttClient;
import com.app.mingluxing.utils.LogUtil;


/**
 * 检测网络是否可用的广播
 * Created by Aaron on 16/3/28.
 */
public class NetReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {

            LogUtil.e("tag", getClass().getName() + "网络不可用!!!!!!!!");

            Toast.makeText(context, context.getResources().getString(R.string.net_off), Toast.LENGTH_SHORT).show();

        } else {
            LogUtil.e("tag", getClass().getName() + "网络可用啦~~~~~~");

            //连接mqtt
            MqttClient mqttClient = MqttManager.getInstance().getMqttClient();

            if (mqttClient == null || !mqttClient.isConnected())
                MqttManager.getInstance().getMqttIp(context.getApplicationContext());
        }


    }

}
